package lib.bone.audit.flow.event.sub;

import lib.bone.access.constant.ResearchTypeConst;
import lib.bone.audit.flow.event.EndEventService;
import lib.bone.nepudata.domain.entity.NepuSubject;
import lib.bone.access.module.dict.entity.NepuSubjectDict;
import lib.bone.access.module.dict.DictServiceCtl;
import lib.bone.audit.workload.domain.NepuWorkload;
import lib.bone.audit.workload.attach.ResearchAttach;
import lib.bone.access.constant.WorkloadSourceConst;
import lib.bone.access.constant.WorkloadTypeConst;
import lib.bone.audit.workload.service.NepuWorkloadService;
import lib.bone.nepudata.service.ResearchServiceCtl;
import lib.bone.common.constant.ResponseCodeConst;
import lib.bone.common.exception.BusinessException;
import lib.bone.common.utils.JsonUtil;
import lib.bone.flow.domain.dto.FlowParam;
import lib.bone.flow.domain.entity.FlowWork;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Optional;

@Service
public class SubjectEndEvent implements EndEventService<NepuSubject, NepuSubjectDict> {
    private static final String TYPE = ResearchTypeConst.SUBJECT;

    @Autowired
    ResearchServiceCtl researchServiceCtl;

    @Autowired
    DictServiceCtl dictServiceCtl;

    @Autowired
    NepuWorkloadService workloadService;

    @Override
    public String getServiceType() {
        return ResearchTypeConst.SUBJECT;
    }

    @Override
    public FlowParam buildFlowParam(FlowParam flowParam) {
        return null;
    }

    @Override
    public NepuWorkload buildWorkload(FlowParam flowParam, NepuSubject subject, NepuSubjectDict nepuSubjectDict) {
        NepuWorkload nepuWorkload = new NepuWorkload();
        // 获取默认工作量
        Double workload = subject.getWorkload();
        nepuWorkload.setWorkload(workload);
        // 设置工作量来源 -- 申请
        nepuWorkload.setWorkloadSource(WorkloadSourceConst.APPLY);
        // 设置工作量类型 -- 科研工作量
        nepuWorkload.setWorkloadType(WorkloadTypeConst.RESEARCH);
        // 设置工作量级别
        String level = nepuSubjectDict.getSubjectLevel();
        nepuWorkload.setWorkloadLevel(level);
        // 设置获取工作量用户ID
        Long userId = subject.getLeaderId();
        nepuWorkload.setUserId(userId);
        // 获取工作量所在周期 -- 从科研项目中提取
        nepuWorkload.setCycleId(subject.getCycleId());

        // 设置父周期 = 自身
        nepuWorkload.setParentId(subject.getCycleId());

        //获取科研项目Id
        nepuWorkload.setLinkId(subject.getId());
        //设置工作量可分配
        nepuWorkload.setWorkloadLock(2);
        // 设置使用工作量为 0 用尽标识为 0
        nepuWorkload.setUsedWorkload(0.0);
        nepuWorkload.setUsedUp(0);
        // 设置工作量附加信息
        String attach = buildAttach(subject,nepuSubjectDict);
        nepuWorkload.setWorkloadAttach(attach);
        // 设置锁等级为 2
        nepuWorkload.setWorkloadLock(2);

        return nepuWorkload;
    }

    @Override
    public NepuSubjectDict getDict(NepuSubject subject) {
        String dictId = subject.getSubjectDictId();
        Optional<Object> dictOp = dictServiceCtl.getById(dictId,TYPE);
        if (!dictOp.isPresent()){
            throw new BusinessException(ResponseCodeConst.NOT_FOUND,"ID:["+dictId+"]对应的横向课题词典未找到");
        }
        return (NepuSubjectDict) dictOp.get();
    }

    @Override
    public NepuSubject getEntity(String id) {
        Optional<Object> entityOp = researchServiceCtl.getById(id,TYPE);
        if (!entityOp.isPresent()){
            throw new BusinessException(ResponseCodeConst.NOT_FOUND,"ID:["+id+"]对应横向课题未找到");
        }
        return (NepuSubject) entityOp.get();
    }

    @Override
    public void process(FlowWork flowWork, FlowParam flowParam) {
        String serviceId = flowParam.getServiceId();
        NepuSubject subject = getEntity(serviceId);
        NepuSubjectDict dict = getDict(subject);
        NepuWorkload workload = buildWorkload(flowParam,subject,dict);
        workloadService.saveOne(workload);
    }

    @Override
    public String buildAttach(NepuSubject subject, NepuSubjectDict nepuSubjectDict) {
        ResearchAttach attach = new ResearchAttach();
        attach.setResearchId(subject.getId());
        attach.setResearchName(subject.getSubjectName());
        attach.setDictId(nepuSubjectDict.getId());
        attach.setDictName(nepuSubjectDict.getSubjectType());
        attach.setResearchLevel(nepuSubjectDict.getSubjectLevel());
        attach.setResearchType(ResearchTypeConst.SUBJECT);
        return JsonUtil.obj2String(attach);
    }
}
